// -------------------------------
// Demos: Form Components
// -------------------------------

$(function() {

     // iPhone like button Toggle
     // $('.toggle').toggles({on:true});

    // Autogrow Textarea
    $('textarea.autosize').autosize({append: "\n"});

    ////Typeahead for Autocomplete
    //$('.example-countries.typeahead').typeahead({
    //  name: 'countries',
    //  prefetch: 'Content/demo/countries.json',
    //  limit: 10
    //});

    //Color Picker
    //$('.cpicker').colorpicker();


    //jQueryUI Date Picker

    $(".datepicker").datepicker({ dateFormat: "d/m/yy"});
    
 
    $('.datepickerbtn').click(function () {
        $(this).parent().children('.datepicker').datepicker("show");
    });
  
    // Date Range Picker
    $(document).ready(function() {
        $('.daterangepicker').daterangepicker();
    });


    //SELECT2

    //For detailed documentation, see: http://ivaynberg.github.io/select2/index.html

    //Populate all select boxes with from select#source
    var opts=$("#source").html(), opts2="<option></option>"+opts;
    $("select.populate").each(function() { var e=$(this); e.html(e.hasClass("placeholder")?opts2:opts); });

    //select2
    $(".select2").select2({width: 'resolve'});
    
//    //Rotten Tomatoes Infinite Scroll + Remote Data example
//    $(".select2-remote").select2({
//        placeholder: "Search for a movie",
//        minimumInputLength: 3,
//        width: 'resolve',
//        ajax: {
//            url: "http://api.rottentomatoes.com/api/public/v1.0/movies.json",
//            dataType: 'jsonp',
//            quietMillis: 100,
//            data: function (term, page) { // page is the one-based page number tracked by Select2
//                return {
//                    q: term, //search term
//                    page_limit: 10, // page size
//                    page: page, // page number
//                    apikey: "q7jnbsc56ysdyvvbeanghegk" // please do not use so this example keeps working
//                };
//            },
//            results: function (data, page) {
//                var more = (page * 10) < data.total; // whether or not there are more results available

//                // notice we return the value of more so Select2 knows if more results can be loaded
//                return {results: data.movies, more: more};
//            }
//        },
//        formatResult: movieFormatResult,
//        formatSelection: movieFormatSelection,
//        dropdownCssClass: "bigdrop", // apply css that makes the dropdown taller
//        escapeMarkup: function (m) { return m; } // we do not want to escape markup since we are displaying html in results
//    });


//  //MULTISELECT2

//  // For detailed documentatin, see: loudev.com

//$('#multi-select2').multiSelect();

//$('#multi-select').multiSelect({
//  selectableHeader: "<input type='text' class='form-control' style='margin-bottom: 10px;'  autocomplete='off' placeholder='Filter entries...'>",
//  selectionHeader: "<input type='text' class='form-control' style='margin-bottom: 10px;' autocomplete='off' placeholder='Filter entries...'>",
//  afterInit: function(ms){
//    var that = this,
//        $selectableSearch = that.$selectableUl.prev(),
//        $selectionSearch = that.$selectionUl.prev(),
//        selectableSearchString = '#'+that.$container.attr('id')+' .ms-elem-selectable:not(.ms-selected)',
//        selectionSearchString = '#'+that.$container.attr('id')+' .ms-elem-selection.ms-selected';

//    that.qs1 = $selectableSearch.quicksearch(selectableSearchString)
//    .on('keydown', function(e){
//      if (e.which === 40){
//        that.$selectableUl.focus();
//        return false;
//      }
//    });

//    that.qs2 = $selectionSearch.quicksearch(selectionSearchString)
//    .on('keydown', function(e){
//      if (e.which == 40){
//        that.$selectionUl.focus();
//        return false;
//      }
//    });
//  },
//  afterSelect: function(){
//    this.qs1.cache();
//    this.qs2.cache();
//  },
//  afterDeselect: function(){
//    this.qs1.cache();
//    this.qs2.cache();
//  }
//  });


});








function movieFormatResult(movie) {
        var markup = "<table class='movie-result'><tr>";
        if (movie.posters !== undefined && movie.posters.thumbnail !== undefined) {
            markup += "<td class='movie-image'><img src='" + movie.posters.thumbnail + "'/></td>";
        }
        markup += "<td class='movie-info'><div class='movie-title'>" + movie.title + "</div>";
        if (movie.critics_consensus !== undefined) {
            markup += "<div class='movie-synopsis'>" + movie.critics_consensus + "</div>";
        }
        else if (movie.synopsis !== undefined) {
            markup += "<div class='movie-synopsis'>" + movie.synopsis + "</div>";
        }
        markup += "</td></tr></table>"
        return markup;
    }

    function movieFormatSelection(movie) {
        return movie.title;
    }